package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.exception.EmployeeNotExistException;
import com.sky.pojo.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        if(employeeLoginVO.getId()==null){
            return Result.error("用户名或密码错误");
        }
        return Result.success(employeeLoginVO);
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /*
    *新增员工
     */
    @PostMapping()
    @ApiOperation("新增员工")
    public Result<T> add(@RequestBody EmployeeDTO employee)
    {
        log.info("{}", employee);
        employeeService.add(employee);
        return Result.success();
    }

    /*
     *分页查询
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result<PageResult> add(EmployeePageQueryDTO employeePageQueryDTO)
    {
        log.info("分页条件查询{}", employeePageQueryDTO);
        PageResult p=employeeService.page(employeePageQueryDTO);
        return Result.success(p);
    }
    /*
    启用或禁用
     */

    @ApiOperation("启用或禁用")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable(name = "status") Integer status, Long id){
        log.warn("启用或禁用, status:{}, id:{}", status, id);
        return employeeService.startOrStop(status, id);
    }
/*
根据ID查询,员工回显
 */
    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable(name = "id") Long id) throws EmployeeNotExistException {
        log.warn("员工回显：{}", id);
        return employeeService.findById(id);
    }
    /**
     * 编辑员工信息
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody Employee employee){
        log.info("编辑员工信息：{}", employee);
        employeeService.update(employee);
        return Result.success();
    }


}
